package com.zzwtec.validation.core;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.zzwtec.validation.AnnotationValidator;
import com.zzwtec.validation.util.BooleanUtil;

/**
 * 运行器验证规则
 * @author  草原狼
 *
 * 2017年8月23日
 */

public class RuntimeValidateRule<T> {
	
	private Class<T> beanClass;
	//验证器集合，为引擎中的一份副本
	private Map<String,AnnotationValidator> validMaps;

	//排除验证器
	private Map<String,Boolean> excludeValid = new HashMap<String,Boolean>();
	//包括验证器
	private Map<String,Boolean> includeValid = new HashMap<String,Boolean>();
	
	
	public RuntimeValidateRule(Map<String, AnnotationValidator> validMaps) {
		this.validMaps = validMaps;		
	}
	
	public RuntimeValidateRule<T> getSelf() {
		return this;
	} 
	
	private Class<T> getBeanClass(){
		if(beanClass == null) {
			throw new RuntimeException("需要在RuntimeValidateRule<T>.SET_BEAN_CLASS(Class<T> beanClass)方法中设置待验证的实体类");
		}
		return this.beanClass;
	}
	
    /**
     * 设置待验证的实体类 
     * @param beanClass
     * @return
     */
	public RuntimeValidateRule<T> setBeanClass(Class<T> beanClass) { 
		this.beanClass = beanClass;   
		return getSelf(); 
	} 
	
	/**
	 * 排除某验证器
	 * 如果该类不具备该验证器，就无需排除
	 * @param fieldName
	 * @return
	 */
	public RuntimeValidateRule<T> exclude(String fieldName,String valid) { 
		Class<T> beanClass = getBeanClass();
		try {
			Field f = beanClass.getDeclaredField(fieldName);
			AnnotationValidator validator = validMaps.get(valid);
			Annotation ann = f.getAnnotation(validator.getAnnotationClass());
			if (ann != null) {
				excludeValid.put(fieldName+"-"+valid, true);
			}
		} catch (NoSuchFieldException e) {
			throw new RuntimeException(beanClass.getName()+"类中没有"+fieldName+"属性");
		} catch (Exception e) {			
			e.printStackTrace();
		}
		
		return getSelf(); 
	} 
	
	/**
	 * 包括某验证器
	 * 如果实体bean指定field没有配置该验证器，就追加该验证器
	 * @param fieldName
	 * @param valid
	 * @return
	 
	public RuntimeValidateRule<T> include(String fieldName,String valid) { 
		Class<T> beanClass = getBeanClass();
		try {
			Field f = beanClass.getDeclaredField(fieldName);
			IAnnotationParser validator = validMaps.get(valid);
			Annotation ann = f.getAnnotation(validator.getAnnotationClass());
			if (ann == null) {
				includeValid.put(fieldName+"-"+valid, true);
			}			
		} catch (NoSuchFieldException e) {
			throw new RuntimeException(beanClass.getName()+"类中没有"+fieldName+"属性");
		} catch (Exception e) {			
			e.printStackTrace();
		}		
		return getSelf(); 
	}
	*/
	
	/**
	 * 是否匹配排除规则
	 * @param fieldName
	 * @param valid
	 * @return
	 */
	public boolean matchExcludeRule(String fieldName,String valid) {
		Boolean value = excludeValid.get(fieldName+"-"+valid);
		return BooleanUtil.isTrue(value);		
	}
	
	/**
	 * 是否匹配包含规则
	 * @param fieldName
	 * @param valid
	 * @return
	 */
	public boolean matchCludeRule(String fieldName,String valid) {
		Boolean value = includeValid.get(fieldName+"-"+valid);
		return BooleanUtil.isTrue(value);		
	}
}
